'use strict';
// 传入length,random,page
// length:需要查询的数量
// page:查寻开始条数
// random：是否随机查找
exports.main = async (event, context) => {
	//event为客户端上传的参数
	let {length,random} = event
  const db = uniCloud.database()
  if(random){
     return  await db.collection("books").aggregate().sample({size: length}).end()
  }
  else{
    let {page} = event
    let count = await db.collection("books").count()
    let data = await db.collection("books").aggregate().sort({ _id:-1}).skip(page).limit(length).end()
    return {
      count,
      data
    }
  }
	//返回数据给客户端
};
